import 'dart:async';
import 'package:flutter/services.dart';

class BytedeskPush {
  //
  static const MethodChannel _methodChannel =
      const MethodChannel('bytedesk_push/methodChannel');
  static const BasicMessageChannel<String> _basicMessageChannel =
      const BasicMessageChannel('bytedesk_push/messageChannel', StringCodec());
  static const EventChannel _eventChannel =
      const EventChannel('bytedesk_push/eventChannel');

  static Future<String> get platformVersion async {
    final String version =
        await _methodChannel.invokeMethod('getPlatformVersion');
    return version;
  }

  // 调用小米推送注册接口
  static void register(String appId, String appKey) async {
    //
    Map<String, Object> params = {"appId": appId, "appKey": appKey};
    await _methodChannel.invokeMethod('registerPush', params);
    // 2. 接收来自Native的消息，并向Native回复
    _basicMessageChannel
        .setMessageHandler((String message) => Future<String>(() {
              print('receive message $message');
              return "收到Native的消息：" + message;
            }));
    //
    _eventChannel.receiveBroadcastStream().listen((Object event) {
      print('on event success $event');
    }, onError: (Object error) {
      print('on event error $error');
    });
  }

  // flutter 发送到 native消息
  static void sendMessage(content) async {
    String response = await _basicMessageChannel.send(content);
    print("收到Native的消息回复：" + response);
  }

  // flutter 发送到 native事件
  static void sendEvent(content) async {

  }

  //
  static void unRegister() async {
    await _methodChannel.invokeMethod('unRegister');
  }

  //
  static void bindAlias(String alias) async {
    Map<String, Object> params = {"alias": alias};
    await _methodChannel.invokeMethod('bindAlias', params);
  }

  //
  static void unBindAlias(String alias) async {
    Map<String, Object> params = {"alias": alias};
    await _methodChannel.invokeMethod('unBindAlias', params);
  }

  //
  static Future<List<String>> getAlias() async {
    return await _methodChannel.invokeMethod('getAlias');
  }

  //
  static void addTag(String tag) async {
    Map<String, Object> params = {"tag": tag};
    await _methodChannel.invokeMethod('addTag', params);
  }

  //
  static void deleteTag(String tag) async {
    Map<String, Object> params = {"tag": tag};
    await _methodChannel.invokeMethod('deleteTag', params);
  }

  //
  static Future<List<String>> getTags() async {
    return await _methodChannel.invokeMethod('getTags');
  }

  //
  // static Future<String> getPushToken() async {
  //   return await _methodChannel.invokeMethod('getPushToken');
  // }

  //
  static void setUserAccount(String userAccount) async {
    Map<String, Object> params = {"userAccount": userAccount};
    await _methodChannel.invokeMethod('setUserAccount', params);
  }

  //
  static void unsetUserAccount(String userAccount) async {
    Map<String, Object> params = {"userAccount": userAccount};
    await _methodChannel.invokeMethod('unsetUserAccount', params);
  }
}
